<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
use DB;
class ManageController extends Controller
{

  public $app = null;
  public function __construct(){
    $options = [
      'debug'  => true,
      'app_id' => 'wx61a5ce5230a32e92',
      'secret' => '14ba21479bce9ff60ba93185cbc5e4bb',
      'token'  => 'johntest',
      // 'aes_key' => null, // 可选
      'log' => [
          'level' => 'debug',
          'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
      ],
      'oauth' => [
         'scopes'   => ['snsapi_base'],
         'callback' => '/logins',
       ],
      'guzzle'=>[
        'timeout'=>5.0,
        'verify'=>false,
      ]
      //...
    ];
    $this->app = new Application($options);
  }
    public function logins(Request $req){
        $oauth = $this->app->oauth;
        $user = $oauth->user();
        $req->session()->put('users', $user);
        //return redirect('/center');
	return $this->tiaos($user);
    }
    public function centers(Request $req){

            $oauth = $this->app->oauth;
            return $oauth->redirect();
            //return redirect('login');
    }

    public function logouts(Request $req){
        $req->session()->forget('users');
        return $this->redirect('/center');
    }
    public function tiaos($id){
	//return $this->https2_request();
        //return view('lijitiaozhuan',['id'=>$id]);
      	$open = $id['id'];
	//$url = "http://1.shhomer.sinaapp.com/output/GetGates.php?openid=$open";
	$url = "http://www.laoyou1.me/shhomer/output/GetGates.php?openid=$open";

	$did = $this->https2_request($url);
	//dd( $did );
      	$did = json_decode($did);
	if (  $did->Status !='SUCCESS' ){
	return redirect("http://www.laoyou1.me/chart1.3/views/manage.php");
	}	
	$did = $did->Data[0][0];
	if( $did == '' ){
		return redirect("http://www.laoyou1.me/chart1.3/views/manage.php?openid=$open&did=SIMCOM_SIM800C861694035953494&gatename=祁阿姨");

	}
	//return redirect("http://www.laoyou1.me/chart/views/manage.php?openid=$open&did=$did");
	//return $did;
	return redirect("http://www.laoyou1.me/chart1.3/views/manage.php?openid=$open&did=$did");

    }
    public function https2_request($url,$data = null){
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	if (!empty($data)){
        	curl_setopt($curl, CURLOPT_POST, 1);
        	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
   	 }
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	$output = curl_exec($curl);
    	curl_close($curl);
    	return $output;

    }

}
